package cc.comac.mom.datahandlers.entity;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cc.comac.mom.util.status.AsrStatusMap;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cc.comac.mom.bean.AsrSearchBean;
import cc.comac.mom.bean.QualityAsrFeatures;
import cc.comac.mom.bean.base.LimitPageBean;
import cc.comac.mom.config.OSSService;
import cc.comac.mom.dao.entity.AsrSearchDao;
import cc.comac.mom.entity.QualityAsrFeaturesEntity;
import cc.comac.mom.util.ExportExcel;
import cc.comac.mom.util.ResultBean;

@Component
public class AsrSearchDataHandler {

	
	@Autowired
	private AsrSearchDao asrSearchDao;
	
	@Autowired
	private OSSService oSService;
	public ResultBean selectBySomeFeilds(AsrSearchBean asrSearch) {
		ResultBean resultBean = new ResultBean(true);
		if (asrSearch.getPageNum() == 0) {
			asrSearch.setPageNum(1);
		}
		if (asrSearch.getPageSize() == 0) {
			asrSearch.setPageSize(10);
		}
		asrSearch.setPageNum((asrSearch.getPageNum()-1)*asrSearch.getPageSize());

		List<AsrSearchBean> selectBySomeFeilds = asrSearchDao.selectBySomeFields(asrSearch);
		int selectCountBySomeFeilds = asrSearchDao.selectCountBySomeFields(asrSearch);
		LimitPageBean<AsrSearchBean> limitPageBean = new LimitPageBean<AsrSearchBean>();
		// 将查询总数除以每页数量后向上取整获取总页数
		double pageCount = Math.ceil((double)selectCountBySomeFeilds / asrSearch.getPageSize());
		limitPageBean.setEntityList(selectBySomeFeilds);
		limitPageBean.setPageCount((int)pageCount);
		limitPageBean.setTotalQuantity((int)selectCountBySomeFeilds);
		resultBean.setObject(limitPageBean);
		return resultBean;
	}
	
	public List<AsrSearchBean> selectAll(AsrSearchBean asrSearch) {

		List<AsrSearchBean> selectBySomeFeilds = asrSearchDao.selectAll(asrSearch);
		return selectBySomeFeilds;
	}
	
	public String exportData(HttpServletRequest request, HttpServletResponse response, AsrSearchBean obj) {
		List<AsrSearchBean> entityList = selectAll(obj);
		// excel中对应的表头
		String[] rowName = new String[] { "ASR号","材料/零件图号", "PO/FO号", "等级", "装箱单号","一批数","状态" };
		List<Object[]> dataList = new ArrayList<Object[]>();
		for (AsrSearchBean asrSearchBean : entityList) {
			Object[] objs = new Object[rowName.length];
			objs[0] = asrSearchBean.getAsrNumber();
			objs[1] = asrSearchBean.getPartNumber();
			objs[2] = asrSearchBean.getFoPoNumber();
			objs[3] = asrSearchBean.getGrade();
			objs[4] = asrSearchBean.getPackingNumber();
			objs[5] = asrSearchBean.getCheckBatchQuantity();
			objs[6] = AsrStatusMap.getStatusCN(asrSearchBean.getStatus());
					// 结论暂定
			dataList.add(objs);
		}

		String fileName = "ASR查询数据";
		// 执行导出
		HSSFWorkbook exportExcel = ExportExcel.exportExcel(request, response, fileName, rowName, dataList,
				"yyyy-MM-dd HH:mm:ss");
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		try {
			exportExcel.write(baos);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ByteArrayInputStream swapStream = new ByteArrayInputStream(baos.toByteArray());
		String updateSheet = oSService.updateSheet(swapStream, fileName);
		return updateSheet;
	}
	
	

}
